home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / updates / update15.zoo / pml / pmlsrc / cdiv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-04  |  2.8 KB  |  117 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *                N O T I C E                *
  4.  *                                    *
  5.  *            Copyright Abandoned, 1987, Fred Fish        *
  6.  *                                    *
  7.  *    This previously copyrighted work has been placed into the    *
  8.  *    public domain by the author (Fred Fish) and may be freely used    *
  9.  *    for any purpose, private or commercial.  I would appreciate    *
  10.  *    it, as a courtesy, if this notice is left in all copies and    *
  11.  *    derivative works.  Thank you, and enjoy...            *
  12.  *                                    *
  13.  *    The author makes no warranty of any kind with respect to this    *
  14.  *    product and explicitly disclaims any implied warranties of    *
  15.  *    merchantability or fitness for any particular purpose.        *
  16.  *                                    *
  17.  ************************************************************************
  18.  */
  19.  
  20.  
  21. /*
  22.  *  FUNCTION
  23.  *
  24.  *    cdiv   double precision complex division
  25.  *
  26.  *  KEY WORDS
  27.  *
  28.  *    cdiv
  29.  *    complex functions
  30.  *    machine independent routines
  31.  *    math libraries
  32.  *
  33.  *  DESCRIPTION
  34.  *
  35.  *    Computes double precision complex result of division of
  36.  *    first double precision complex argument by second double
  37.  *    precision complex argument.
  38.  *
  39.  *  USAGE
  40.  *
  41.  *    COMPLEX cdiv (numerator, denominator)
  42.  *    COMPLEX numerator;
  43.  *    COMPLEX denominator;
  44.  *
  45.  *  PROGRAMMER
  46.  *
  47.  *    Fred Fish
  48.  *    Tempe, Az 85281
  49.  *    (602) 966-8871
  50.  *
  51.  *  INTERNALS
  52.  *
  53.  *    Computes cdiv(znum,zden) from:
  54.  *
  55.  *        1.    Let znum = a + j b
  56.  *            Let zden = c + j d
  57.  *
  58.  *        2.    denom = c*c + d*d
  59.  *
  60.  *        3.    If denom is zero then log error,
  61.  *            set r_cdiv = maximum floating value,
  62.  *            i_cdiv = 0, and go to step 5.
  63.  *
  64.  *        4.    r_cdiv = (a*c + b*d) / denom
  65.  *            i_cdiv = (c*b - a*d) / denom
  66.  *
  67.  *        5.    Then cdiv(znum,zden) = r_cdiv + j i_cdiv
  68.  *
  69.  */
  70.  
  71. #if defined (__M68881__) && !defined (_M68881)
  72. /*# define _M68881*/
  73. #endif
  74.  
  75. #include <stdio.h>
  76. #include <math.h>
  77. #include "pml.h"
  78.  
  79. COMPLEX cdiv (znum, zden)
  80. COMPLEX znum;
  81. COMPLEX zden;
  82. {
  83.     COMPLEX result;
  84.     double denom;
  85.     struct exception xcpt;
  86.  
  87.     result.real = ((znum.real*zden.real)+(znum.imag*zden.imag));
  88.     result.imag = ((zden.real*znum.imag)-(znum.real*zden.imag));
  89.     denom = (zden.real * zden.real) + (zden.imag * zden.imag);
  90.  
  91.     if (denom == 0.0) {
  92. #ifdef    ERROR_CHECK
  93.     xcpt.type = SING;
  94.     xcpt.name = "cdiv";
  95.     xcpt.arg1 = denom;
  96.     if (!matherr (&xcpt)) {
  97.         fprintf (stderr, "%s:  ZERO_CMPLX_DENOMINATOR \n", xcpt.name);
  98.         xcpt.retval = 0.0;    /* useless in this context */
  99.         errno = ERANGE;    /* should be EDOM if real or imag == 0    */
  100.     }
  101.     if( result.real >= 0.0)    result.real = HUGE_VAL;    
  102.                     /* still wrong, == 0 should yield NAN */
  103.     else            result.real = -HUGE_VAL;    
  104.     if( result.imag >= 0.0) result.imag = HUGE_VAL;
  105.                     /* still wrong, == 0 should yield NAN */
  106.     else            result.imag = -HUGE_VAL;    
  107. #else    ERROR_CHECK
  108.     result.real /= denom;
  109.     result.imag /= denom;
  110. #endif    ERROR_CHECK
  111.     } else {
  112.     result.real /= denom;
  113.     result.imag /= denom;
  114.     }
  115.     return (result);
  116. }
  117.